/*
 * @author mzw
 * 
 */
	if(typeof MZW=="undefined"){var MZW={};};
	if(typeof MZW.widget=="undefined"){MZW.widget={};};
	MZW.widget.TableTree=function(id, oConfigs){
		 if (id){
		  	 this.init(id);
		  	 this.createEvent("rowClick");
		  	 this.createEvent("rowDbClick");
		  	 this.createEvent("checkboxClick");
			 this.subscribe("expand", function(node){
			 	//node.resetChildrenCheckbox();
			 });
		 }
		 if(oConfigs){
		 	this._headers=oConfigs["headers"];
			this._schema=oConfigs["schema"];
			this.tableWidth=this.calculateWidth();
			this.selectedRowColor=oConfigs["selectedRowColor"];
			this._checkbox=oConfigs["checkbox"];
			this._compareColIndex=oConfigs["compareColIndex"]?oConfigs["compareColIndex"]:0;
		 }
	};
	YAHOO.extend(MZW.widget.TableTree, YAHOO.widget.TreeView, {
		selectedRowColor:"#99cc00",
		_headers:[],
		_schema:[],
		selectedNode:null,
		tableWidth:0,
		_compareColIndex:0,
		changeSchema:function(newSchema){
			if(newSchema instanceof Array && newSchema.length==this._schema.length){
				this._schema=newSchema;
				this.getRoot().refresh();
			}
		},
		//override TreeView draw method
		draw:function(){
			var html = this.root.getHtml();
			var sb=[];
			sb[sb.length] = '<div class="';
			sb[sb.length] = this.getTTHeaderStyle()+'"';
			sb[sb.length] =' style="width:';
			sb[sb.length] =this.tableWidth+'"';
			sb[sb.length] = '>';
			sb[sb.length] = this.getHeaderHtml();
			sb[sb.length] ='</div>';
			sb[sb.length] ='<div class="';
			sb[sb.length] =this.getTTBodyStyle()+'"';
			sb[sb.length] =' style="width:';
			sb[sb.length] =this.tableWidth+'"';
			sb[sb.length] ='>';
			sb[sb.length] =html;
			sb[sb.length] ='</div>';
			var hc=sb.join("");
        	this.getEl().innerHTML = hc;
        	this.firstDraw = false;
		},
		//generate  header for tabletree
		getHeaderHtml:function(){
	        var sb = [];
	        sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0">';
			sb[sb.length] = '<tr class="';
			sb[sb.length] = this.getHeaderStyle()+'">';
			var h=this._headers;
			var s=this._schema;
			for(var i=0;i<h.length;i++){
				sb[sb.length] = '<td class="';
				sb[sb.length] = this.getHeaderCellStyle(i)+'"';
				sb[sb.length] = ' style="width:';
				sb[sb.length] = h[i].width;
				sb[sb.length] = ';">';			
				sb[sb.length] = h[i].text;
				sb[sb.length] = '</td>';
			}
			sb[sb.length] = '<td class="';
			sb[sb.length] = this.getHeaderCellStyle(i)+'"';
			sb[sb.length] = ' style="width:16px';
			sb[sb.length] = ';">';			
			sb[sb.length] = '&nbsp;';
			sb[sb.length] = '</td>';	
			sb[sb.length] = '</tr>';
			sb[sb.length] = '</table>';
			return sb.join("");
		},
		getTTContainerStyle:function(){
			return "ttContainer";
		},
		getTTHeaderStyle:function(){
			return "ttHeaderContainer";
		},
		getTTBodyStyle:function(){
			return "ttBody";
		},
		getHeaderCellStyle:function(index){
			return (index==0)?"ttFirstHeaderCell" : "ttHeaderCell";
		},
		getHeaderStyle:function(){
			return "ttHeader";
		},
		//calculate width of tabletree
		calculateWidth:function(){
			var h=this._headers;
			var w=0;
			for(var i=0;i<h.length;i++){
				w+=parseInt(h[i].width);
			}
			w+=20;//add width of border and scrollbar 
			//w+=12;//add spacer width
			return w+="px";
		},
		//get node by id of tr;
		getNodeByRowElId:function(rowElId){
			var nodes=this._nodes;
			for(var i=0;i<nodes.length;i++){
				if(nodes[i] && nodes[i].getRowElId()==rowElId){
					return nodes[i];
				}
			}
			return null;
		},
		onRowClick:function(node){
			this.selectedNode=node?node:null;
			this.selectedNode.getRowEl().style.background=this.selectedRowColor;
			var nodes=this._nodes;
			for(var i=0;i<nodes.length;i++){
				var n=nodes[i];
				if(n && n!=node){
					var e=n.getRowEl();
					if(e){
					e.style.background='transparent';
					}
				}
			}	
			this.fireEvent("rowClick", node);
		},
		onRowDbClick:function(node){
			this.fireEvent("rowDbClick", node);
		},
		onCheckboxClick:function(node){
			this.fireEvent("checkboxClick", node);
		},
		addOrUpdateNode:function(data, parentNode, expanded){
			if(!expanded){
				expanded=false;
			}
			var nodes=this._nodes;
			var hasExisit=false;
			var newNode=null;
			
			var p=this._schema[this._compareColIndex];
			var n=this.getNodeByProperty(p, data[p]);
			if(n!=null){
				n.setData(data);
				hasExisit=true;
				newNode=n;
				parentNode=n.parent;
			}
			/*2007-06-20 replace by above 
			for(var i=0;i<nodes.length;i++){
				var n=nodes[i];
				var firstCol=this._schema[0];
				if(n && n.data[firstCol]==data[firstCol]){
					n.setData(data);
					hasExisit=true;
					newNode=n;
					parentNode=n.parent;
					//this.removeNode(n);
				}
			}*/
			if(!parentNode){
				parentNode=this.getRoot();
			}
			if(!hasExisit){
				newNode=new MZW.widget.TableTreeNode(data, parentNode, expanded);
			}
			parentNode.refresh();
			if(!parentNode.expanded){
				parentNode.expand();
			}else{
				parentNode.collapse();
				parentNode.expand();
			}
			return newNode;
		},
		getAllDatas:function(){
			var datas=[];
			var nodes=this.getAllNodes();
			for(var i=0;i<nodes.length;i++){
				datas[datas.length]=nodes[i].data;
			}
			return datas;
		},
		getAllNodes:function(){
			var nodes=this._nodes;
			var ns=[];
			for (var i in this._nodes) {
	            var n = this._nodes[i];
				if(n instanceof MZW.widget.TableTreeNode){
	           		ns[ns.length]=n;
				}
        	}
			return ns;
		},
		resetCheckbox:function(){
			if(this._checkbox===true){
				var s=this._schema;
				var p=s[s.length-1];
				var ns=this.getNodesByProperty(p, true);
				if(ns!=null){
					for (var i in ns) {
						var node=ns[i];	
						if(node && node.data){
			          		node.data[p]=false;
							node.resetChildrenCheckbox();
					  	}
		        	}
					this.getRoot().refresh();
					this.collapseAll();
				}
			}
		}
	});
	
	
	MZW.widget.TableTreeNode=function(oData, oParent, expanded){
	    if (oData) {
			if(oParent.tree._checkbox){
				expanded=false;
			}
			
	        this.init(oData, oParent, expanded);
			this._schema=this.tree._schema;
			var len=this.tree._schema.length-1;
			if(oData[this.tree._schema[len]]===true){
				this.expanded=true;
			}
			this._oData=oData;
			this.firstColWidth=parseInt(this.tree._headers[0].width);
			this.setUpLabel(this._oData[this.tree._schema[0]]);
			
		}
		if(oParent==this.tree.getRoot()){
			this.level= 1;
		}else{
			this.level=oParent.level+1;
		}
	};
	
	
	YAHOO.extend(MZW.widget.TableTreeNode, YAHOO.widget.TextNode, {
		toString:function(){
			var desc="Node (" + this.index + ")";
			desc+=" data{";
			var s=this.tree._schema;
			var d=this._oData;
			var len=s.length;
			for(var i=0;i<len;i++){
				desc+=(i<len-1)? (d[s[i]]+",") : d[s[i]];
			}
			desc+="}";
			return desc;
		},
		setData:function(data){
			var sc=this.tree._schema;
			var s=sc[sc.length-1];
			if(this._oData[s]!=data[s]){
				this.onCheckboxClick({checked:data[s]});
			}
			this.data=data;
			this._oData=data;
		},
		_oData:{},
		hasChildNodes:false,
		onRowClick:function(){
			this.tree.onRowClick(this);
		},
		onRowDbClick:function(){
			this.tree.onRowDbClick(this);
		},
		onCheckboxClick:function(chk){
			if(chk.checked){
				this.expand();
				//this.resetChildrenCheckbox();
			}else{
				this.collapse();
				this.resetChildrenCheckbox();
			}
			var sc=this.tree._schema;
			this.data[sc[sc.length-1]]=chk.checked;
			this.tree.onCheckboxClick(this);
		},
		onNormalCheckboxClick:function(chk, index){
			var sc=this.tree._schema;
			this.data[sc[index]]=chk.checked;
		},
		getRowElId:function(){
			return "ttRow"+this.index;
		},
		getRowEl:function(){
			return document.getElementById(this.getRowElId());
		},
		resetChildrenCheckbox:function(){
			if(this.tree._checkbox===true){
				var s=this.tree._schema;
				var p=s[s.length-1];
				var ns=this.getChilrenByProperty(p, true);
				if(ns!=null){
					for (var i in ns) {
						var node=ns[i]; 
						if(node && node.data){
			        	node.data[p]=false;
						node.resetChildrenCheckbox();
						}
		        	}
					this.refresh();
					this.collapse();
				}
			}
		},
		getChilrenByProperty: function(property, value) {
        	var values = [];
        	for (var i in this.children) {
            	var n = this.children[i];
            	if (n.data && value == n.data[property]) {
                	values.push(n);
            	}
        	}

       		return (values.length) ? values : null;
    	},
		
		getNodeHtml: function() { 
	        var sb = [];
	 		var getNode = 'YAHOO.widget.TreeView.getNode(\'' +
	                        this.tree.id + '\',' + this.index + ')';
							
	        sb[sb.length] = '<table border="0" cellpadding="0" cellspacing="0"';
			sb[sb.length] = ' class="'+this.getTreeNodeStyle();
			sb[sb.length] = '">';
	        sb[sb.length] = '<tr';
			sb[sb.length] = ' id="' +this.getRowElId() + '"';
			sb[sb.length] = ' isTTRow=true';
			sb[sb.length] = ' onclick="';
			sb[sb.length] = getNode+'.onRowClick();"';
			sb[sb.length] = ' ondblclick="';
			sb[sb.length] = getNode+'.onRowDbClick();"';
			sb[sb.length] = ' onmouseover="this.className=';
			sb[sb.length] = getNode+'.getRowHoverStyle();"';
			sb[sb.length] = ' onmouseout="this.className=';
			sb[sb.length] = getNode+'.getRowUnHoverStyle();"';
			sb[sb.length] = '>';
	        
	        for (var i=0;i<this.depth;++i) {
	            sb[sb.length] = '<td class="' + this.getDepthStyle(i) + '"><div class="ygtvspacer"></div></td>';
	        }
	
	        sb[sb.length] = '<td';
	        //sb[sb.length] = ' onselectstart="return false"';
	        sb[sb.length] = ' id="' + this.getToggleElId() + '"';
	        sb[sb.length] = ' class="' + this.getStyle() + '"';
	        if (this.hasChildren(true)) {
	            sb[sb.length] = ' onmouseover="this.className=';
	            sb[sb.length] = getNode + '.getHoverStyle()"';
	            sb[sb.length] = ' onmouseout="this.className=';
	            sb[sb.length] = getNode + '.getStyle()"';
	        }
			if(!this.tree._checkbox){
	        	sb[sb.length] = ' onclick="javascript:' + this.getToggleLink();
			}
			sb[sb.length] = '">';
	        sb[sb.length] = '<div class="ygtvspacer">';
	
	        sb[sb.length] = '</div>';
	        sb[sb.length] = '</td>';
	        sb[sb.length] = '<td class="';
			sb[sb.length] = this.getColStyle()+'"';
			sb[sb.length] = ' style="width:';
			sb[sb.length] = this.getFirstColWidth()+';"';
	        sb[sb.length] = (this.nowrap) ? ' nowrap="nowrap" ' : '';
	        sb[sb.length] = ' >';
	        sb[sb.length] = '<a';
	        sb[sb.length] = ' id="' + this.labelElId + '"';
	        sb[sb.length] = ' class="' + this.labelStyle + '"';
			sb[sb.length] = ' style="width:' + this.getFirstColWidth() + ';"';
			
			if(!this.tree._checkbox){
	        	sb[sb.length] = ' href="' + this.href + '"';
				sb[sb.length] = ' onclick="return ' + getNode + '.onLabelClick(' + getNode +')"';
			}else{
				sb[sb.length] = ' href="javascript:;"';
			}
	        sb[sb.length] = ' target="' + this.target + '"';
			
	        if (this.hasChildren(true)) {
	            sb[sb.length] = ' onmouseover="document.getElementById(\'';
	            sb[sb.length] = this.getToggleElId() + '\').className=';
	            sb[sb.length] = getNode + '.getHoverStyle()"';
	            sb[sb.length] = ' onmouseout="document.getElementById(\'';
	            sb[sb.length] = this.getToggleElId() + '\').className=';
	            sb[sb.length] = getNode + '.getStyle()"';
	        }
	        sb[sb.length] = ' >';
	        sb[sb.length] = this.label;
	        sb[sb.length] = '</a>';
	        sb[sb.length] = '</td>';
			
			sb[sb.length]=this.getOtherColsHtml();
			
			sb[sb.length] = '</tr>';
	        sb[sb.length] = '</table>';
	
	        return sb.join("");
		},
		getOtherColsHtml:function(){
			var getNode = 'YAHOO.widget.TreeView.getNode(\'' +
                        this.tree.id + '\',' + this.index + ')';
			var sb=[];
			var count=this.tree._schema.length;
			var headers=this.tree._headers;
			for(var ix=1; ix<count; ix++){
				sb[sb.length] = '<td class="';
				sb[sb.length] = this.getColStyle()+'"';
				sb[sb.length] = ' style="width:'+headers[ix].width+';"';
				sb[sb.length] = (this.nowrap) ? ' nowrap="nowrap" ' : '';
				sb[sb.length] = '>';
				
				if(ix==count-1 && this.tree._checkbox){
					sb[sb.length] = '<input type="checkbox"';
					sb[sb.length] = ' class="'+this.getCheckboxStyle()+'"';
					sb[sb.length] = ' onclick="';
					sb[sb.length] = getNode+'.onCheckboxClick(this);"';
					if(this._oData[this.tree._schema[ix]]==true){
						sb[sb.length] = ' checked="true"';
					}
					sb[sb.length] = '/>';
				}else{
					sb[sb.length] = this.createCellContent(headers[ix].type, ix);
					//sb[sb.length] = this._oData[this.tree._schema[ix]];
				}
				sb[sb.length] = '</td>';
			}
			return sb.join('');
		},
		createCellContent:function(type, index){
			var getNode = 'YAHOO.widget.TreeView.getNode(\'' +
                        this.tree.id + '\',' + this.index + ')';
			var sb=[];
			var t=type?type.toLowerCase():"";
			switch(t){
				case "text":
					sb[sb.length] = this._oData[this.tree._schema[index]];
					break;
				case "checkbox":
					sb[sb.length] = '<input type="checkbox"';
					sb[sb.length] = ' class="'+this.getCheckboxStyle()+'"';
					sb[sb.length] = ' onclick="';
					sb[sb.length] = getNode+'.onNormalCheckboxClick(this,'+ index+');"';
					if(this._oData[this.tree._schema[index]]===true){
						sb[sb.length] = ' checked="true"';
					}
					sb[sb.length] = '/>';
					break;
				default:
					sb[sb.length] = this._oData[this.tree._schema[index]];
			}
			return sb.join('');
		},
		getStyle:function(){
			if (this.isLoading) {
           	 	return "ygtvloading";
	        } else {
	            // location top or bottom, middle nodes also get the top style
	            var loc = (this.nextSibling) ? "t" : "l";
	
	            // type p=plus(expand), m=minus(collapase), n=none(no children)
	            var type = "n";
	        	if (this.hasChildren(true) || (this.isDynamic() && !this.getIconMode())|| this.hasChildNodes) {
	            // if (this.hasChildren(true)) {
	             type = (this.expanded) ? "m" : "p";
	        	}
				
	
	        	return "ygtv" + loc + type;
       		}
		},
		getFirstColWidth:function(){
			return this.firstColWidth-(16*this.level)+'px';
		},
		getTreeNodeStyle:function(){
			return "ttRow";
		},
		getColStyle:function(){
			return "ttCol";
		},
		getRowHoverStyle:function(){
			return "ttRowHover";
		},
		getRowUnHoverStyle:function(){
			return "ttRowUnHover";
		},
		getCheckboxStyle:function(){
			return "ttCheckBox";
		}
	}); 
	